﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChatDBAccess;

namespace Twitter
{
    public partial class UserChat : System.Web.UI.Page
    {
        string name;
        int receiverId;

        protected void Page_Load(object sender, EventArgs e)
        {
            name = Request.QueryString["name"];
            receiverId = Search.FindUserByName(name);
            if (Session["userId"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                int id = (int)Session["userId"];
                var res = Search.MessagesBetweenUsers(id, receiverId);
                foreach (var mess in res)
                {
                    Literal l = new Literal();
                    l.Text = "<h4> Posted by " + mess.Name + " on " + mess.Message.PostDate + "</h4>" + "<p>" + mess.Message.Text + "</p>" + "<hr />";
                    messagesHolder.Controls.Add(l);
                }
            }
        }

        protected void postNewMsgBtn_Click(object sender, EventArgs e)
        {
            if (Session["userId"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                int id = (int)Session["userId"];
                Create.CreateUTUMessage(id, receiverId, Server.HtmlEncode(newMsgBox.Text));
                Response.Redirect("~/UserChat.aspx?name=" + name);
            }
        }
    }
}